<template>
  <div>
    <div class="main">
      <eagle-input
        name="calendar"
        v-model="form.PlanName"
        label="预案名称"
        placeholder="请输入"
        required
        isInputText
      >
      </eagle-input>
      <eagle-input
        name="CheckTypeName"
        label="预案类型"
        v-model="form.PlanTypeName"
        required
        :islink="true"
        readonly
        @click="show=true"
        placeholder="请选择检查类型"
      ></eagle-input>
      <eagle-date
        label="实施时间"
        required
        :date.sync="form.EffectDate"
      ></eagle-date>
      <eagle-file-upload
        @CallBack="_fileUploadCb"
        :attachs="form.Attachs"
        :labelName="'预案文件'"
        :mustInput="true"
        labelWidth="100"
      ></eagle-file-upload>

      <div class="bottom-btns">
        <eagle-button type="primary" class="submit-btn" @click="save()">
          保存
        </eagle-button>
      </div>
    </div>
    <!--检查类型-->
    <check-type-dialog
      v-model="show"
      :data="form"
      @close="closeCheckType"
    ></check-type-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import UserChooseDialog from "@/views/components/user/userChooseDialog.vue";
import checkTypeDialog from "@/views/components/site/checkTypeDialog.vue";
export default {
  components: {
    UserChooseDialog,
    checkTypeDialog,
  },
  data() {
    return {
      id: this.$route.query.id,
      userChooseDialogShow1: false,
      form: { Details: [] },
      show: false,
      plantypeShow: false,
      PlanTypeName:'',
      PlanType:'',

    };
  },

  computed: {
    ...mapGetters(["baseUrl", "CompanyCode", "roles"]),
  },

  created() {
    if (this.$route.query.id != null) {
      this.id = this.$route.query.id
    }
    if (this.id == 0) {
      var d = new Date();
      this.form.Year = d.getFullYear().toString();
      this.getInitEntity()
    }else{
      this.getDetail();
    }
    
  
  },
  methods: {
    _fileUploadCb(attachs) {
      this.form.Attachs = attachs;
    },
    getDetail() {
      let params = { id: this.id };
      this.$axios
        .get("/SitePlan/GetDetail", {
          params: params,
        })
        .then((res) => {
          this.form = res.data;
          this.PlanType=res.data.PlanType
          res.data.PlanTypeList.forEach(element => {
            if(element.ID==this.PlanType){
              this.form.PlanTypeName=element.Name
            }

          });
        });
    },
    getInitEntity() {
      let params = { id: this.id };
      this.$axios
        .get("/SitePlan/InitEntity", {
          params: params,
        })
        .then((res) => {
          this.form = res.data;
        });
    },
    // 选择检查类型
    closeCheckType(type) {
      console.log(type)
      if (type) {
        if (type.indexOf("Custom") >= 0) {
          this.data.CheckTypeName = type.substring(7);
        } else {
          this.form.PlanTypeList.forEach((item) => {
            if (item.ID == type) {
              this.form.PlanTypeName = item.Name;
            }
          });
        }
        this.form.PlanType = type;
      }
      this.show = false;
    },


    save() {
      if (!this.form.PlanName) {
        return this.$toast("请选择预案名称");
      }

      if (!this.form.EffectDate) {
        return this.$toast("请选择时间");
      }

      if (!this.form.Attachs) {
        return this.$toast("请选择预案文件");
      } else {
        if (this.form.Attachs.length == 0) {
          return this.$toast("请选择预案文件");
        }
      }
      // ID=[64]&PlanName=[现场处置方案]&PlanType=[C]&EffectDate=[2023-11-28]&Attachs=[[{"AttCode":"CA23110700001","AttExt":".xls","AttName":"立沙岛企业名单"}]]
      this.$axios
        .post("/SitePlan/Edit", this.form)
        .then((res) => {
          this.$toast.success("保存成功");
          setTimeout(() => {
            this.$router.goRefresh(-1);
          }, 300);
        });
    },
  },
};
</script>

<style lang="less" >
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.block {
  width: 120px;
  height: 120px;
  background-color: #fff;
}
</style>
<style lang="less" scoped>
.space-between {
  justify-content: space-between;
}
.bottom-btns {
  margin-top: 10px;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .submit-btn {
    width: 100%;
    height: 35px;
    font-size: 14px;
  }
}
</style>
